﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using System.Web;

namespace LiangYeeProj
{
    public class DataManager
    {
        //private string exchange = "DCE";
        //private string[] symbols = { "jm", "i", "p" };

        private string exchange = "SHFE";
        private string[] symbols = { "rb", "al", "zn", "ni", "au", "ag", "bu", "ru" };
        //private string[] symbols = { "cu"};

        //private string exchange = "CZCE";
        //private string[] symbols = { "SR" };

        private string startDate = "2013-05-01";
        private string endDate = "2017-05-31";

        public static string[] GetTradingDay(string startDate, string endDate)
        {
            string url = "http://future.liangyee.com/bus-api/future/futureInfo/getTradingDay?userKey=01A38020F82B43B0B9194A8921585C41&startDate=" + startDate + "&endDate=" + endDate;
            string data = DownloadUrl(url);
            LiangYeeTradingDay lyData = JsonConvert.DeserializeObject<LiangYeeTradingDay>(data);
            return lyData.result;
        }
        public static string DownloadUrl(string url)
        {
            string data;
            using (WebClient client = new WebClient())
            {
                client.Encoding = Encoding.UTF8;
                try
                {
                    data = client.DownloadString(url);
                }catch(Exception ex)
                {
                    Console.WriteLine("download error {0}", ex.Message);
                    data = "";
                }

            }
            return data;
        }

        public static void SaveTick2TXT(string startDate, string endDate, string symbol, string exchange)
        {
            string dirPath = "F:\\liangyee_tick\\" + exchange +"_"+ symbol;
            if (!Directory.Exists(dirPath))
                Directory.CreateDirectory(dirPath);
            string filePath = dirPath + "\\"+ symbol + startDate.Replace("-", "") + "_" + endDate.Replace("-", "") + ".txt";

            FileStream fs;
            if (!File.Exists(filePath))
                fs = new FileStream(filePath, FileMode.Create);
            else
                fs = new FileStream(filePath, FileMode.Append);
            StreamWriter sw = new StreamWriter(fs);

            DateTime t1 = DateTime.Now;
            DateTime t2 = DateTime.Now;

            Console.WriteLine("get tradingday.");
            string[] tds = GetTradingDay(startDate, endDate);

            int lines = 0;

            for (int i = 0; i < tds.Length; i++)
            {
                string url = string.Format("http://future.liangyee.com/bus-api/future/{0}/getMarketData?userKey=01A38020F82B43B0B9194A8921585C41&product={1}&tradingday={2}", exchange,symbol, tds[i]);
                Console.WriteLine("get date tick :{0}:{1}",symbol, tds[i]);
                t1 = DateTime.Now;
                if ((t2 - t1).Milliseconds < 350)
                    Thread.Sleep(350);

                LiangYeeTick oneDaytick;
                string data = null;
                do
                {
                    if (data != null)
                    {
                        Console.WriteLine("Refresh get data: {0}:{1}", symbol, tds[i]);
                        Thread.Sleep(1000);
                    }
                    data = DownloadUrl(url);
                    oneDaytick = JsonConvert.DeserializeObject<LiangYeeTick>(data);

                } while (data=="" || oneDaytick == null);
                t2 = DateTime.Now;
                if (oneDaytick.code == "200004")
                    continue;
                while (oneDaytick.result == null)
                {
                    Console.WriteLine("Refresh get data: {0}:{1}",symbol, tds[i]);
                    Thread.Sleep(350);
                    data = DownloadUrl(url);
                    oneDaytick = JsonConvert.DeserializeObject<LiangYeeTick>(data);
                }
                for (int j = 0; j < oneDaytick.result.Count; j++)
                {
                    string[] lineArr = oneDaytick.result[j].Split(',');
                    if(lineArr.Length == 18)
                    {
                        if(Convert.ToInt32(lineArr[9]) > 0)
                        {
                            DateTime lineDate = Convert.ToDateTime(lineArr[0] + " " + lineArr[16]);
                            if (lineDate.Hour > 15)
                            {
                                if (lineDate.DayOfWeek == DayOfWeek.Monday)
                                    lineDate = lineDate.AddDays(-3);
                                else
                                    lineDate = lineDate.AddDays(-1);
                            }
                            sw.WriteLine(string.Format("{0}.{1},{2},{3},{4},{5},{6},{7}", lineDate, Convert.ToInt16(lineArr[17]).ToString("000"), lineArr[2], lineArr[9], lineArr[11], lineArr[14], lineArr[15], lineArr[1]));

                            lines++;
                            if (lines % 1000000 == 0)
                                Console.WriteLine("wrote {0} lines", lines);
                        }
                    }
                    else
                        Console.WriteLine("ERROR");
                }
                Console.WriteLine("get date tick {0}:{1} ... sucessful", symbol, tds[i]);
            }
            //清空缓冲区
            sw.Flush();
            //关闭流
            sw.Close();
            fs.Close();

            File.Move(filePath, filePath.Replace(".txt", "") + "_" + lines + "lines.txt");
            Console.WriteLine("save txt successful");
            //Console.ReadKey();
        }
        public void SaveTick2TXT()
        {
            foreach(var symbol in symbols)
                SaveTick2TXT(startDate, endDate, symbol, exchange);
        }


        public static void ReadLastLine(string filePath,int n)
        {
            FileStream fs = new FileStream(filePath, FileMode.Open);
            StreamReader r = new StreamReader(fs);
            List<string> lst = new List<string>();
            while (r.Peek() > -1)
            {

                lst.Add(r.ReadLine());
                if (lst.Count > n)
                    lst.RemoveAt(0);
            }
            fs.Close();
            r.Close();
            foreach (string tmpstr in lst)
            {
                Console.WriteLine(tmpstr);
            }
        }

        public static void DelTxtLine(string startDate, string endDate, string filePath)
        {
            FileStream rFS= new FileStream(filePath, FileMode.Open);
            StreamReader sr = new StreamReader(rFS);

            string newFilePath= filePath.Replace(".txt","") + "_new.txt";
            FileStream wFS;
            wFS = new FileStream(newFilePath, FileMode.Create);
            StreamWriter sw = new StreamWriter(wFS);
            Console.WriteLine("start:");
            while (sr.Peek() > -1)
            {
                string line = sr.ReadLine().Replace("\r", string.Empty).Replace("\n", string.Empty);
                DateTime lineDate = Convert.ToDateTime(line.Substring(0, 10));
                if (lineDate < Convert.ToDateTime(startDate) || lineDate> Convert.ToDateTime(endDate))
                {
                    sw.WriteLine(line);
                }
            }

            sr.Close();
            rFS.Close();


            sw.Flush();
            //关闭流
            sw.Close();
            wFS.Close();

        }
        public static void RepairLiangYeeDateTime(string filePath ,string targetName="")
        {
            FileStream rFS = new FileStream(filePath, FileMode.Open);
            StreamReader sr = new StreamReader(rFS);

            string newFilePath = filePath.Replace(".txt", "") + "_date_repair.txt";
            if (targetName != "")
            {
                newFilePath = Path.Combine(Path.GetDirectoryName(filePath), targetName);
            }
            FileStream wFS;
            wFS = new FileStream(newFilePath, FileMode.Create);
            StreamWriter sw = new StreamWriter(wFS);
            Console.WriteLine("repair start:");
            string line = string.Empty;
            int i = 0;
            while (sr.Peek() > -1)
            {
                line = sr.ReadLine().Replace("\r", string.Empty).Replace("\n", string.Empty);
                i++;
                if (i % 1000000 == 0)
                {
                    Console.WriteLine("read {0} lines", i);
                    Console.WriteLine(line);
                }
                //string[] lineArr = line.Split('.');

                //string[] splits = line.Split('.');
                //string date = splits[0];
                //string data = splits[1];
                int index = line.IndexOf('.');
                string date = line.Substring(0, index);
                string data = line.Substring(index + 1);
                DateTime lineDate = Convert.ToDateTime(date);

                if (lineDate.Hour > 15)
                {
                    if (lineDate.DayOfWeek == DayOfWeek.Monday)
                        lineDate = lineDate.AddDays(-3);
                    else
                        lineDate = lineDate.AddDays(-1);
                    line = lineDate.ToString("yyyy-MM-dd HH:mm:ss") + "." + data; //data 包含毫秒和以后的数据000,3343, 323
                }
                sw.WriteLine(line);
            }
            Console.WriteLine("total lines :{0};   and last line is {1}", i, line);
            sr.Close();
            rFS.Close();


            sw.Flush();
            //关闭流
            sw.Close();
            wFS.Close();
            File.Move(newFilePath, newFilePath.Replace(".txt", "") + "_"+ i + "lines.txt");
        }

    }
}
